<template>
  <div style="padding: 10px;width: 100%">
    <!--  功能区   -->
    <div style="margin: 10px 0">
      <el-button type="primary" size="normal" @click="add">新增</el-button>
    </div>
    <!--  搜索区   -->
    <div style="margin: 10px 0;padding-left: 10px" size="normal">
      <el-date-picker
          v-model="date"
          value-format="YYYY-MM-DD"
          type="date"
          placeholder="请选择一天"
          size="normal"
      />
      <el-input v-model="search" placeholder="请输入关键字" style="margin-left: 5px;width: 20%" size="normal" clearable/>
      <el-button type="primary" style="margin-left: 5px" size="normal"  @click="load">搜索</el-button>
    </div>

    <el-table :data="tableData" stripe border style="width: 100%">
      <el-table-column prop="id" label="ID" sortable width="70px" v-if="false"/>
      <el-table-column prop="user" label="用户"  v-if="false"/>
      <el-table-column prop="dateTime" label="日期时间"  width="120px"  />
      <el-table-column prop="content" label="内容"  width="800px"  />
      <el-table-column prop="privilege" label="权限" width="70px" >
        <template #default="scope">
          <span v-show="scope.row.privilege=='open'">公开</span>
          <span v-show="scope.row.privilege=='private'">私人</span>
        </template>
      </el-table-column>
      <el-table-column prop="comment" label="注释" width="200px" />
      <el-table-column fixed="right" label="Operations" >
        <template #default="scope">
          <el-button  size="small"  @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm title="确认删除吗？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button size="small"  type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin: 10px 0">
      <el-pagination
          :current-page="currentPage"
          :page-size="pageSize"
          :page-sizes="[5, 10, 20]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>

    <el-dialog v-model="dialogVisible" title="新增日志" width="50%">
      <el-form ref="form" :model="form" label-width="120px" :rules="rules">
        <el-form-item label="内容" style="width: 80%" prop="content">
          <el-input type="textarea" v-model="form.content" :maxlength="500" :autosize="{ minRows: 10, maxRows: 10}" />
        </el-form-item>
        <el-form-item label="注释" style="width: 80%;">
          <el-input v-model="form.comment"  />
        </el-form-item>
        <el-form-item label="权限" style="width: 80%;">
          <el-select v-model="form.privilege" placeholder="请选择权限" >
            <el-option label="公开" value="open"/>
            <el-option label="私人" value="private"/>
          </el-select>
        </el-form-item>
        <el-form-item label="时间" style="width: 80%;">
          <el-date-picker
              v-model="form.dateTime"
              type="datetime"
              placeholder="选择日期"
              :shortcuts="shortcuts"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save()">确定</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script>


import request from "@/utils/request";
import moment from 'moment';

export default {
  name: 'JournalView',
  components: {
  },
  data(){
    return{
      form:{},
      user:{},
      dialogVisible:false,
      total:11,
      currentPage:1,
      pageSize:20,
      search:"",
      statistic:"",
      sum:0,
      date: new Date(),
      tableData: [],
      shortcuts : [
        {text: '今天', value: new Date(),},
        {text: '昨天', value: () => {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24)
            return date
          },
        },
        {text: '一周前', value: () => {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
            return date
          },
        },
      ],
      rules:{
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' },
        ]
      },
    }
  },
  computed: {
    formattedDate() {
      // 将currentDate转换为指定格式的字符串
      return moment(this.date).format('YYYY-MM-DD');
    }
  },
  created(){
    let userStr= sessionStorage.getItem("user") || "{}"
    let user = JSON.parse(userStr)
    this.user=user;
    this.load();
  },
  methods:{
    load(){
      //加载信息
      request.get("/journal",{
        params:{
          username:this.user.username,
          pageNum:this.currentPage,
          pageSize:this.pageSize,
          search:this.search,
          date:this.date
        }
      }).then(res =>{
        console.log(res)
        this.tableData = res.data.records
        this.total = res.data.total;
      })
    },
    add(){
      this.dialogVisible=true;
      this.form={};//清空一下表单域
      this.form.id=null;
      this.form.duration=25;
      this.form.dateTime=this.date;
      this.form.privilege='private';
    },
    save(){
      this.$refs['form'].validate(valid=> {
        if (valid) {
          this.form.user=this.user.username
          if(this.form.id){ //更新
            request.put("/journal",this.form).then(res =>{
              console.log(res)
              if(res.code==0){
                this.$message.success("更新成功");
                this.dialogVisible=false;
                this.load();
              }else{
                this.$message.error(res.msg);
              }
            })
          }else{ //新增
            request.post("/journal",this.form).then(res =>{
              console.log(res)
              if(res.code==0){
                this.$message.success("新增成功");
                this.dialogVisible=false;
                this.load();
              }else{
                this.$message.error(res.msg);
              }
            })
          }
        }
      })
    },
    handleEdit(row){
      this.form = JSON.parse(JSON.stringify(row)) //深度拷贝
      this.dialogVisible=true;
    },
    handleDelete(id){
      console.log(id)
      request.delete("/journal/"+id).then(res =>{
        console.log(res)
        if(res.code==0){
          this.$message.success("删除成功");
          this.load();
        }else{
          this.$message.error(res.msg);
        }
      })
    },
    handleSizeChange(pageSize){ //改变当前每页的个数
      this.pageSize=pageSize;
      this.load();
    },
    handleCurrentChange(pageNum){//改变当前页码
      this.currentPage=pageNum;
      this.load();
    },
  }
}
</script>
